#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_CONTACTS 100

// 初始化通讯录
void initAddressBook(AddressBook *book) {
    book->count = 0;
}


// 定义联系人结构体
typedef struct {
    char name[50];
    char position[50];
    char title[50];
    char phone[20];
    char email[50];
} Contact;

// 定义通讯录结构体
typedef struct {
    Contact contacts[MAX_CONTACTS];
    int count;
} AddressBook;


// 添加联系人
void addContact(AddressBook *book) {
    if (book->count >= MAX_CONTACTS) {
        printf("通讯录已满，无法添加新联系人！\n");
        return;
    }
    Contact newContact;
    printf("请输入姓名: ");
    scanf("%s", newContact.name);
    printf("请输入职务: ");
    scanf("%s", newContact.position);
    printf("请输入职称: ");
    scanf("%s", newContact.title);
    printf("请输入手机: ");
    scanf("%s", newContact.phone);
    printf("请输入Email: ");
    scanf("%s", newContact.email);
    book->contacts[book->count++] = newContact;
    printf("联系人添加成功！\n");
}

// 删除联系人
void deleteContact(AddressBook *book) {
    char name[50];
    printf("请输入要删除的联系人姓名: ");
    scanf("%s", name);
    int found = 0;
    for (int i = 0; i < book->count; i++) {
        if (strcmp(book->contacts[i].name, name) == 0) {
            for (int j = i; j < book->count - 1; j++) {
                book->contacts[j] = book->contacts[j + 1];
            }
            book->count--;
            found = 1;
            printf("联系人删除成功！\n");
            break;
        }
    }
    if (!found) {
        printf("未找到该联系人！\n");
    }
}

// 修改联系人信息
void modifyContact(AddressBook *book) {
    char name[50];
    printf("请输入要修改的联系人姓名: ");
    scanf("%s", name);
    int found = 0;
    for (int i = 0; i < book->count; i++) {
        if (strcmp(book->contacts[i].name, name) == 0) {
            printf("请输入新的职务: ");
            scanf("%s", book->contacts[i].position);
            printf("请输入新的职称: ");
            scanf("%s", book->contacts[i].title);
            printf("请输入新的手机: ");
            scanf("%s", book->contacts[i].phone);
            printf("请输入新的Email: ");
            scanf("%s", book->contacts[i].email);
            found = 1;
            printf("联系人信息修改成功！\n");
            break;
        }
    }
    if (!found) {
        printf("未找到该联系人！\n");
    }
}

// 显示所有联系人信息
void displayContacts(AddressBook *book) {
    if (book->count == 0) {
        printf("通讯录为空！\n");
        return;
    }
    printf("姓名\t职务\t职称\t手机\tEmail\n");
    for (int i = 0; i < book->count; i++) {
        printf("%s\t%s\t%s\t%s\t%s\n", book->contacts[i].name, book->contacts[i].position, book->contacts[i].title, book->contacts[i].phone, book->contacts[i].email);
    }
}

// 分类查找联系人
void searchContact(AddressBook *book) {
    char keyword[50];
    printf("请输入要查找的关键字: ");
    scanf("%s", keyword);
    int found = 0;
    printf("姓名\t职务\t职称\t手机\tEmail\n");
    for (int i = 0; i < book->count; i++) {
        if (strstr(book->contacts[i].name, keyword) || strstr(book->contacts[i].position, keyword) ||
            strstr(book->contacts[i].title, keyword) || strstr(book->contacts[i].phone, keyword) ||
            strstr(book->contacts[i].email, keyword)) {
            printf("%s\t%s\t%s\t%s\t%s\n", book->contacts[i].name, book->contacts[i].position, book->contacts[i].title, book->contacts[i].phone, book->contacts[i].email);
            found = 1;
        }
    }
    if (!found) {
        printf("未找到相关联系人！\n");
    }
}

// 按姓名排序并显示联系人
void sortContacts(AddressBook *book) {
    for (int i = 0; i < book->count - 1; i++) {
        for (int j = i + 1; j < book->count; j++) {
            if (strcmp(book->contacts[i].name, book->contacts[j].name) > 0) {
                Contact temp = book->contacts[i];
                book->contacts[i] = book->contacts[j];
                book->contacts[j] = temp;
            }
        }
    }
    printf("按姓名排序后的联系人信息:\n");
    displayContacts(book);
}

// 主菜单
void showMenu() {
    printf("\n通讯录管理系统\n");
    printf("1. 添加联系人\n");
    printf("2. 删除联系人\n");
    printf("3. 修改联系人信息\n");
    printf("4. 显示所有联系人信息\n");
    printf("5. 分类查找联系人\n");
    printf("6. 按姓名排序并显示联系人\n");
    printf("7. 退出\n");
    printf("请输入你的选择: ");
}

int main() {
    AddressBook book;
    initAddressBook(&book);
    int choice;
    do {
        showMenu();
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                addContact(&book);
                break;
            case 2:
                deleteContact(&book);
                break;
            case 3:
                modifyContact(&book);
                break;
            case 4:
                displayContacts(&book);
                break;
            case 5:
                searchContact(&book);
                break;
            case 6:
                sortContacts(&book);
                break;
            case 7:
                printf("退出通讯录管理系统！\n");
                break;
            default:
                printf("无效的选择，请重新输入！\n");
        }
    } while (choice != 7);
    return 0;
}    